//
//  DeTableViewController.m
//  GaoBiaoB
//
//  Created by lanou3g on 16/1/18.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "DeTableViewController.h"
#import "Nuomishuo.h"
#import "Cityqu.h"
#import "SmallCity.h"
#import "HomePageTableViewController.h"
//城市
NSString *urlStr = @"http://apis.baidu.com/baidunuomi/openapi/districts";

@interface DeTableViewController ()

//存放二级城市的字典
@property(nonatomic,strong)NSMutableDictionary *subDict;

//存放二级城市的区名
@property(nonatomic,strong)NSMutableArray *subArray;

@end

NSString *de = @"detailcellIdentifier";
@implementation DeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self requestDataForSub];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:de];
}
-(void)requestDataForSub{
    NSString *ci_ty = [NSString stringWithFormat:@"city_id=%@",_su_b];
    Nuomishuo *nuo = [[Nuomishuo alloc]init];
    [nuo request:urlStr withHttpArg:ci_ty];
    __weak typeof(nuo)temp = nuo;
    dispatch_async(dispatch_get_main_queue(), ^{
        nuo.upData = ^(){
            
            for (Cityqu * subCity in temp.array) {
                
                NSString *key = subCity.district_name;
                
                for (NSDictionary *small in subCity.biz_areas) {
                    SmallCity *model = [SmallCity new];
                    [model setValuesForKeysWithDictionary:small];
                    //要是组不存在那么重新创建
                    if (self.subDict[key] == nil) {
                        NSMutableArray *groupArray = [NSMutableArray array];
                        [groupArray addObject:model];
                        [self.subDict setObject:groupArray forKey:key];
                    }else{
                        NSMutableArray *groupArray = self.subDict[key];
                        [groupArray addObject:model];
                    }
                }
            }
            self.subArray =[NSMutableArray arrayWithArray:self.subDict.allKeys];
            [self.tableView reloadData];
        };
    });
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return _subArray.count;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [NSString stringWithFormat:@"%@",_subArray[section]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [_subDict[self.subArray[section]]count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:de forIndexPath:indexPath];
    NSArray *array = _subDict[self.subArray[indexPath.section]];
    SmallCity *model = array[indexPath.row];
    cell.textLabel.text = model.biz_area_name;
    return cell;
}
//点击具体地点之后触发的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HomePageTableViewController *home =[[HomePageTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
    SmallCity *city = _subDict[_subArray[indexPath.section]][indexPath.row];
    home.smallCityID = city.biz_area_id;
    home.midCityID = self.su_b;
    [self.navigationController pushViewController:home animated:YES];
}
-(NSMutableDictionary *)subDict{
    if (_subDict == nil) {
        _subDict = [NSMutableDictionary dictionary];
    }
    return _subDict;
}

@end
